#![allow(unused_variables)]
use sqlx::Error;
use super::CountNum;
use super::GoodsTypeBean;
use super::GoodsTypeQueryRequest;
use super::Table;

impl<'c> Table<'c, GoodsTypeBean> {
pub async fn activate_goods_type(&self,id:i64) ->Result<u64, Error> {
		let result = sqlx::query(
	r#"update cs_goods_types set active = true where id = ? "#
		)
		.bind(id)
		.execute(&(&*self.pool).clone().unwrap())
		.await;
		match result {
			Ok(result) => {
				Ok(result.rows_affected())
			},
			Err(err) => {
				Err(err)
			}
		}
}

pub async fn add_goods_type(&self,goods_type:GoodsTypeBean) ->Result<u64, Error> {
		let result = sqlx::query(
		r#"insert into cs_goods_types ( name,active) values (?,?)"#
		)
		.bind(goods_type.name)
		.bind(goods_type.active)
		.execute(&(&*self.pool).clone().unwrap())
		.await;
		match result {
			Ok(result) => {
				Ok(result.rows_affected())
			},
			Err(err) => {
				Err(err)
			}
		}
}

pub async fn count_active_goods_type_records(&self) ->Result<CountNum, Error> {
		let result = sqlx::query_as(
		r#"select count(*) as countNum from cs_goods_types where active = true"#
		)
		.fetch_one(&(&*self.pool).clone().unwrap())
		.await;
		return result;
}

pub async fn count_search_goods_types_by_fields_records(&self,goods_type_query_request:GoodsTypeQueryRequest) ->Result<CountNum, Error> {
		let mut query = r#"select count(*) as countNum from cs_goods_types where 1=1 "#.to_string();
		let name = goods_type_query_request.name.unwrap_or_default();
		if !name.is_empty() {
			let query_name  = format!("{}{}{}"," and name like '%",name,"%' ");
			query = format!("{} {}", query, query_name);
		}
		let active = goods_type_query_request.active.unwrap_or_default();
		if !active.is_empty() && (active=="true" || active =="false"){
			let query_active  = format!("{}{}{}"," and active = ",active," ");
			query = format!("{} {}", query, query_active);
		}
		let result = sqlx::query_as(&query)
		.fetch_one(&(&*self.pool).clone().unwrap())
		.await;
		return result;
}

pub async fn delete_goods_type(&self,id:i64) ->Result<u64, Error> {
		let result = sqlx::query(
	r#"delete from cs_goods_types where id = ? "#
		)
		.bind(id)
		.execute(&(&*self.pool).clone().unwrap())
		.await;
		match result {
			Ok(result) => {
				Ok(result.rows_affected())
			},
			Err(err) => {
				Err(err)
			}
		}
}

pub async fn find_goods_type_by_id(&self,id:i64) ->Result<GoodsTypeBean, Error> {
	let result = sqlx::query_as(
	r#"select id,name,active from cs_goods_types where id = ? "#
		)
		.bind(id)
		.fetch_one(&(&*self.pool).clone().unwrap())
		.await;
		return result;
}

pub async fn find_goods_type_by_name(&self,name:String) ->Result<GoodsTypeBean, Error> {
	let result = sqlx::query_as(
	r#"select id,name,active from cs_goods_types where name = ?;"#
		)
		.bind(name)
		.fetch_one(&(&*self.pool).clone().unwrap())
		.await;
		return result;
}

pub async fn list_active_goods_types(&self) ->Result<Vec<GoodsTypeBean>, Error> {
	let result = sqlx::query_as(
	   r#"select id,name,active from cs_goods_types where active = true order by id asc"#
	)
	.fetch_all(&(&*self.pool).clone().unwrap())
	.await;
	return result;
}

pub async fn list_all_goods_types(&self) ->Result<Vec<GoodsTypeBean>, Error> {
	let result = sqlx::query_as(
	   r#"select id,name,active from cs_goods_types"#
	)
	.fetch_all(&(&*self.pool).clone().unwrap())
	.await;
	return result;
}

pub async fn search_goods_types_by_fields(&self,goods_type_query_request:GoodsTypeQueryRequest) ->Result<Vec<GoodsTypeBean>, Error> {
	let mut query = r#"select id,name,active from cs_goods_types where 1=1 "#.to_string();
	let name = goods_type_query_request.name.unwrap_or_default();
	if !name.is_empty() {
		let query_name  = format!("{}{}{}"," and name like '%",name,"%' ");
		query = format!("{} {}", query, query_name);
	}
	let active = goods_type_query_request.active.unwrap_or_default();
	if !active.is_empty() && (active=="true" || active =="false"){
		let query_active  = format!("{}{}{}"," and active = ",active," ");
		query = format!("{} {}", query, query_active);
	}
	let result = sqlx::query_as(&query)
	.fetch_all(&(&*self.pool).clone().unwrap())
	.await;
	return result;
}

pub async fn search_goods_types_by_fields_by_page(&self,goods_type_query_request:GoodsTypeQueryRequest) ->Result<Vec<GoodsTypeBean>, Error> {
		let mut query = r#"select id,name,active from cs_goods_types where 1=1 "#.to_string();
		let name = goods_type_query_request.name.unwrap_or_default();
		if !name.is_empty() {
			let query_name  = format!("{}{}{}"," and name like '%",name,"%' ");
			query = format!("{} {}", query, query_name);
		}
		let active = goods_type_query_request.active.unwrap_or_default();
		if !active.is_empty() && (active=="true" || active =="false"){
			let query_active  = format!("{}{}{}"," and active = ",active," ");
			query = format!("{} {}", query, query_active);
		}
		let pagenum =  goods_type_query_request.page.unwrap_or_default();
		let pagesize =  goods_type_query_request.rows.unwrap_or_default();
		let start = (pagenum - 1) * pagesize;
		let limit = pagesize;
		query = format!("{} limit {} offset {} ", query, limit, start);
		let result = sqlx::query_as(&query)
		.fetch_all(&(&*self.pool).clone().unwrap())
		.await;
		return result;
}

pub async fn soft_delete_goods_type(&self,id:i64) ->Result<u64, Error> {
		let result = sqlx::query(
		r#"update cs_goods_types set active = false where id = ? "#
		)
		.bind(id)
		.execute(&(&*self.pool).clone().unwrap())
		.await;
		match result {
			Ok(result) => {
				Ok(result.rows_affected())
			},
			Err(err) => {
				Err(err)
			}
		}
}

pub async fn toggle_goods_type(&self,id:i64) ->Result<u64, Error> {
		let result = sqlx::query(
	r#"update cs_goods_types set active = not active where id = ? "#
		)
		.bind(id)
		.execute(&(&*self.pool).clone().unwrap())
		.await;
		match result {
			Ok(result) => {
				Ok(result.rows_affected())
			},
			Err(err) => {
				Err(err)
			}
		}
}

pub async fn update_goods_type(&self,goods_type:GoodsTypeBean) ->Result<u64, Error> {
		let result = sqlx::query(
		r#"update cs_goods_types set name = ? ,active = ? where id = ? "#
		)
		.bind(goods_type.name)
		.bind(goods_type.active)
		.bind(goods_type.id)
		.execute(&(&*self.pool).clone().unwrap())
		.await;
		match result {
			Ok(result) => {
				Ok(result.rows_affected())
			},
			Err(err) => {
				Err(err)
			}
		}
}

pub async fn find_goods_type_indexed_name(&self,short_name:String) ->Result<Vec<GoodsTypeBean>, Error> {
	let result = sqlx::query_as(
	r#"select id,name,active from cs_goods_types where name like concat(? ,'%') "#
	)
	.bind(short_name)
	.fetch_all(&(&*self.pool).clone().unwrap())
	.await;
		return result;
}

}
